package br.upe.pso;

import java.util.ArrayList;
import java.util.Random;


public class Particle {
	private double[] position;
	private double[] velocity;
	private double fitness;
	private double lBestFitness;
	private double pBestFitness;
	private double[] pBestPos;
	private double[] lBestPos;//position
	private ArrayList<Particle> neigbors;	
	
	public Particle() {
		this.position = new double[Constants.DIMENSIONS];
		this.velocity = new double[Constants.DIMENSIONS];
		this.lBestPos = this.position;
		this.pBestPos = this.position;
		this.lBestFitness = this.lBestFitness;
		this.pBestFitness = 99999;
		initializeParticle(Constants.DIMENSIONS);
		this.fitness = 0;
		this.neigbors = new ArrayList<Particle>();
	}
	
	private void initializeParticle(int dimensionNumber) {
		// TODO Auto-generated method stub
		Random generate = new Random();
		for (int i=0; i<dimensionNumber; i++) {
			this.position[i] = generate.nextDouble() * 15 + 15;
			this.velocity[i] = 0.0;
		}
	}
	
	
	public double getPosition(int dim) {
		return position[dim];
	}

	public void setPosition(int dim, double value) {
		this.position[dim] = value;
	}

	public double getFitness() {
		return fitness;
	}

	public void setFitness(double fitness) {
		this.fitness = fitness;
	}

	public double getVelocity(int dim) {
		return velocity[dim];
	}

	public void setVelocity(int dim, double value) {
		this.velocity[dim] = value;
	}

	public double[] getPosition() {
		return position;
	}

	public void setPosition(double[] position) {
		this.position = position;
	}

	public double[] getVelocity() {
		return velocity;
	}

	public void setVelocity(double[] velocity) {
		this.velocity = velocity;
	}

	public double getlBestFitness() {
		return lBestFitness;
	}

	public void setlBestFitness(double lBestFitness) {
		this.lBestFitness = lBestFitness;
	}

	public double[] getlBestPos() {
		return lBestPos;
	}

	public void setlBestPos(double[] lBestPos) {
		this.lBestPos = lBestPos;
	}

	public void setpBestFitness(double pBestFitness) {
		this.pBestFitness = pBestFitness;
	}

	public void setpBestPos(double[] pBestPos) {
		this.pBestPos = pBestPos;
	}

	public void setNeigbors(ArrayList<Particle> neigbors) {
		this.neigbors = neigbors;
	}

	public double getpBestPos(int dim) {
		return pBestPos[dim];
	}
	
	public double[] getpBestPos() {
		return pBestPos;
	}
	
	public double getpBestFitness()
	{
		return this.pBestFitness;
	}

	public void setpBestPos(int dim, double value) {
		this.pBestPos[dim] = value;
	}

	public Particle getNeigbors(int dim) {
		return neigbors.get(dim);
	}
	
	public ArrayList<Particle> getNeigbors() {
		return neigbors;
	}
	
	public void addNeigbors(Particle neigbors) {
		this.neigbors.add(neigbors);
	}
	
	public void updateLBest(){
		//VERIFICA O PROPRIO PBEST
		if(this.pBestFitness > this.lBestFitness){
			this.lBestPos = this.pBestPos.clone();
			this.lBestFitness = this.pBestFitness;
		}
		//VERIFICA OS PBESTS DOS VIZINHOS
		for(int i=0;i<this.neigbors.size();i++){
			Particle neighboor = this.neigbors.get(i);
			if(neighboor.getpBestFitness()>this.lBestFitness){
				this.lBestPos = neighboor.getpBestPos().clone();
				this.lBestFitness = neighboor.getpBestFitness();
			}
		}
	}
}
